package ozu.project;

import ozu.project.continent.OzuContinent;
import ozu.project.regions.OzuRegion;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;


public class OzuMap implements AbstractMap{
    private OzuRegion[] regions = {
            new OzuRegion("ISTANBUL"),       // 0
            new OzuRegion("BURSA"),// 1
            new OzuRegion("BARTIN"),      // 2
            new OzuRegion("ANKARA"),         // 3
            new OzuRegion("IZMIR"),  // 4
            new OzuRegion("KAYSERI"),        // 5
            new OzuRegion("ADANA"),        // 6
            new OzuRegion("TOKAT"),      // 7
            new OzuRegion("MANISA"),      // 8
            new OzuRegion("MUGLA"),         // 9
            new OzuRegion("ANTALYA"),      // 10
            new OzuRegion("VAN"),       // 11
            new OzuRegion("ERZURUM"),         // 12
            new OzuRegion("TRABZON")              };

    //Indexes:
    private static final int ISTANBUL = 0;
    private static final int BURSA = 1;
    private static final int BARTIN = 2;
    private static final int ANKARA = 3;
    private static final int IZMIR = 4;
    private static final int KAYSERI = 5;
    private static final int ADANA = 6;
    private static final int TOKAT = 7;
    private static final int MANISA = 8;
    private static final int MUGLA = 9;
    private static final int ANTALYA = 10;
    private static final int VAN = 11;
    private static final int ERZURUM = 12;
    private static final int TRABZON = 13;

    private static final int MARMARA = 2;
    private static final int AEGEAN = 4;
    private static final int MEDITERRANEAN = 3;
    private static final int BLACK_SEA = 3;
    private static final int CENTRAL_ANATOLIA = 4;
    private static final int EAST_VALUE = 3;

    private OzuPlayer[] players;

    private OzuContinent marmaraR = new OzuContinent("Marmara", MARMARA, Color.DARK_GRAY,
            new OzuRegion[]{regions[ISTANBUL], regions[BURSA]});
    private OzuContinent aegean = new OzuContinent("Aegean", AEGEAN, Color.cyan,
            new OzuRegion[]{regions[IZMIR], regions[MANISA], regions[MUGLA]});
    private OzuContinent mediterraneanSea = new OzuContinent("Mediterranean", MEDITERRANEAN, Color.MAGENTA, new OzuRegion[]{
            regions[ANTALYA], regions[ADANA]
    });
    private OzuContinent blackSea = new OzuContinent("Black Sea", BLACK_SEA, Color.orange,
            new OzuRegion[]{regions[BARTIN], regions[TOKAT], regions[TRABZON]});
    private OzuContinent centralAnatolia = new OzuContinent("Central Anatolia", CENTRAL_ANATOLIA, Color.blue,
            new OzuRegion[]{regions[ANKARA], regions[KAYSERI]});
    private OzuContinent eastAnatolia = new OzuContinent("East", EAST_VALUE, Color.red,
            new OzuRegion[]{regions[ERZURUM], regions[VAN]});

    private OzuContinent[] continents = new OzuContinent[]{
    		marmaraR,
    		aegean,
    		mediterraneanSea,
    		blackSea,
    		centralAnatolia,
    		eastAnatolia
    };

    public OzuMap(ArrayList<String> playerNames) {
        Random randGen = new Random();
        players = new OzuPlayer[playerNames.size()];
        int i = 0;

        Color[] playerColors = new Color[]{Color.yellow, Color.green,
                Color.cyan, Color.magenta, Color.orange, Color.pink};

        while (!playerNames.isEmpty()){
            int randInt = randGen.nextInt(playerNames.size());
            players[i] = new OzuPlayer(playerNames.get(randInt), playerColors[i]);
            playerNames.remove(randInt);
            i++;
        }
        players[0].setActive(true); 

        ArrayList<Integer> listOfRegionIndexes = new ArrayList<>();
        for (int j = 0; j < regions.length; j++) {
            listOfRegionIndexes.add(j);
        }
        while (!listOfRegionIndexes.isEmpty()) {
            for (OzuPlayer player : players) {
                int random = randGen.nextInt(listOfRegionIndexes.size());
                int randomRegionIndex = listOfRegionIndexes.get(random);
                player.addRegion(
                        regions[randomRegionIndex]);
                listOfRegionIndexes.remove(random);
                if (listOfRegionIndexes.isEmpty()){
                    break;
                }
            }
        }

        addRelations(new int[][]{
                {ISTANBUL,BURSA},
                {ISTANBUL,BARTIN},
                {BURSA,BARTIN},
                {BURSA,IZMIR},
                {IZMIR,MANISA},
                {BARTIN,TOKAT},
                {BARTIN,MANISA},
                {ANKARA,MANISA},
                {ANKARA,ADANA},
                {ANKARA,TOKAT},
                {ANKARA,ANTALYA},
                {MANISA,MUGLA},
                {IZMIR,MUGLA},
                {MUGLA,ANTALYA},
                {ANTALYA,ADANA},
                {KAYSERI,ADANA},
                {KAYSERI,TOKAT},
                {VAN,KAYSERI},
                {VAN,ADANA},
                {ERZURUM,TOKAT},
                {ERZURUM,TRABZON},
                {TRABZON,TOKAT}
        });
    }

    public OzuRegion[] getRegions() {
        return regions;
    }

    public OzuPlayer[] getPlayers() {
        return players;
    }

    public OzuContinent[] getContinents() {
        return continents;
    }

    private void addRelation(int region1, int region2) {
        regions[region1].addNeighbour(regions[region2]);
        regions[region2].addNeighbour(regions[region1]);
    }

    private void addRelations(int[][] relations){
        for (int[] relation : relations) {
            addRelation(relation[0], relation[1]);
        }
    }
}
